﻿#region Using Directives

using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

#endregion

namespace FeatureBuilder.ContribLib.Commands
{
    /// <summary>
    /// Feature Extension Command that does adds an assembly reference to a Project.
    /// </summary>
    [DisplayName("Add Solution Folder item")]
    [Description("Adds an Item to a Solution folder.")]
    [Category("IDE")]
    public class AddSolutionFolderItemFromFile : FeatureCommand
    {
        public string SourceFilePath { get; set; }
        public SolutionFolder SolutionFolder { get; set; }
        public string RelativePath { get; set; }
        public bool Copy { get; set; }
        public bool Open { get; set; }

        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }

        #region Overrides

        public override void Execute()
        {
            ProjectItem outputProjectItem = default(ProjectItem);

            try
            {
                DTE dte = this.ServiceProvider.GetService(typeof(DTE)) as DTE;

                if (Copy)
                {
                    if (!string.IsNullOrEmpty(RelativePath))
                    {
                        string destinationPath = string.Concat(Path.GetDirectoryName(dte.Solution.Properties.Item("Path").Value.ToString()), RelativePath, Path.GetFileName(SourceFilePath));

                        File.Copy(SourceFilePath, destinationPath, true);

                        outputProjectItem = SolutionFolder.Parent.ProjectItems.AddFromFile(destinationPath);
                    }
                    else
                    {
                        outputProjectItem = SolutionFolder.Parent.ProjectItems.AddFromFileCopy(SourceFilePath);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(RelativePath))
                    {
                        string destinationPath = string.Concat(Path.GetDirectoryName(dte.Solution.Properties.Item("Path").Value.ToString()), RelativePath, Path.GetFileName(SourceFilePath));

                        outputProjectItem = SolutionFolder.Parent.ProjectItems.AddFromFile(destinationPath);
                    }
                    else
                    {
                        outputProjectItem = SolutionFolder.Parent.ProjectItems.AddFromFile(SourceFilePath);
                    }
                }

                if (Open && outputProjectItem != null)
                {
                    Window wnd = outputProjectItem.Open(Constants.vsViewKindPrimary);
                    wnd.Visible = true;
                    wnd.Activate();
                }
            }
            catch
            {
                if (outputProjectItem != null)
                {
                    outputProjectItem.Delete();
                }
            }
        }

        #endregion
    }
}
